#import "SMCategoryBarView.h"
#import <Masonry/Masonry.h>

@interface SMCategoryBarView ()
@property(nonatomic, strong) UIScrollView *scroll;
@property(nonatomic, strong) UIView *indicator;
@property(nonatomic, strong) NSMutableArray<UIButton *> *buttons;
@property(nonatomic, assign) NSInteger selectedIndex;
@end

@implementation SMCategoryBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _scroll = [UIScrollView new];
        _scroll.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scroll];
        [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        _indicator = [UIView new];
        _indicator.backgroundColor = [UIColor systemBlueColor];
        _indicator.layer.cornerRadius = 1.5;
        [_scroll addSubview:_indicator];
        _buttons = [NSMutableArray array];
        self.backgroundColor = UIColor.systemBackgroundColor;
    }
    return self;
}

- (void)setTitles:(NSArray<NSString *> *)titles {
    _titles = [titles copy];
    for (UIView *v in _scroll.subviews) {
        if (v != _indicator) [v removeFromSuperview];
    }
    [_buttons removeAllObjects];

    CGFloat x = 12;
    for (NSInteger i=0;i<titles.count;i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:titles[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor labelColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor systemBlueColor] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [btn sizeToFit];
        CGRect f = btn.frame;
        f.origin.x = x;
        f.origin.y = 0;
        f.size.height = 44;
        f.size.width += 10;
        btn.frame = f;
        btn.tag = i;
        [btn addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
        [_scroll addSubview:btn];
        [_buttons addObject:btn];
        x += f.size.width + 8;
    }
    _scroll.contentSize = CGSizeMake(x, 44);
    [self setSelectedIndex:0 animated:NO];
}

- (void)tap:(UIButton *)btn {
    [self setSelectedIndex:btn.tag animated:YES];
    if (self.onSelect) self.onSelect(btn.tag);
}

- (void)setSelectedIndex:(NSInteger)index animated:(BOOL)animated {
    if (index < 0 || index >= self.buttons.count) return;
    _selectedIndex = index;
    for (UIButton *b in self.buttons) b.selected = (b.tag == index);

    UIButton *sel = self.buttons[index];
    CGFloat underlineW = MAX(24, sel.titleLabel.intrinsicContentSize.width);
    CGRect indF = CGRectMake(CGRectGetMinX(sel.frame) + (sel.frame.size.width - underlineW)/2.0,
                             44-3, underlineW, 3);
    if (animated) {
        [UIView animateWithDuration:0.25 animations:^{
            self.indicator.frame = indF;
        }];
    } else {
        self.indicator.frame = indF;
    }
    CGFloat targetX = CGRectGetMidX(sel.frame) - self.bounds.size.width/2.0;
    targetX = MAX(0, MIN(targetX, self.scroll.contentSize.width - self.bounds.size.width));
    [self.scroll setContentOffset:CGPointMake(targetX, 0) animated:animated];
}

- (void)updateIndicatorFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    if (fromIndex < 0 || toIndex < 0 ||
        fromIndex >= self.buttons.count || toIndex >= self.buttons.count) return;

    UIButton *fromB = self.buttons[fromIndex];
    UIButton *toB   = self.buttons[toIndex];

    CGFloat fromW = MAX(24, fromB.titleLabel.intrinsicContentSize.width);
    CGFloat toW   = MAX(24, toB.titleLabel.intrinsicContentSize.width);
    CGFloat fromX = CGRectGetMinX(fromB.frame) + (fromB.frame.size.width - fromW)/2.0;
    CGFloat toX   = CGRectGetMinX(toB.frame)   + (toB.frame.size.width   - toW)/2.0;

    CGFloat curX = fromX + (toX - fromX) * progress;
    CGFloat curW = fromW + (toW - fromW) * progress;

    self.indicator.frame = CGRectMake(curX, 44-3, curW, 3);
}
@end
